<?php

namespace App\Http\Controllers\Activity;

use App\Entities\Activity;
use App\Morefun\Wechat\Service as WechatService;

class PublishingApiController
{
    public function getAppPublishingUrl($activityId)
    {
        $activity = Activity::with('model', 'template')->find($activityId);

        if (empty($activity)) {
            return response()->failed('活动不存在');
        }

        if ($activity->template === null) {
            return response()->failed('活动不合法');
        }

        $host = config('api.host');
        $query = http_build_query(['act_id' => $activityId, 'ways' => 'app']);
        $url = $host . $activity->template->url . '?' . $query;

        return response()->success(['published_url' => $url]);
    }

    public function getWechatPublishingUrl(WechatService $service, $activityId)
    {
        $activity = Activity::query()->where('id', $activityId)->first();

        if (!$activity) {
            return response()->failed('活动不存在');
        }

        $url = $service->makeSilentlyAuthorizedUrl($activityId);

        return response()->success(['published_url' => $url]);
    }
}
